<?php
class NewsController extends AppController {

	public function beforeFilter() {
        parent::beforeFilter();
  	
        //create table in database for this exemple
      		 /* Load Model datasource */
      			App::import('Model', 'ConnectionManager');
      			$con = new ConnectionManager;
      			$cn = $con->getDataSource('default');
            /* message table schema */
      			$sql = "CREATE TABLE IF NOT EXISTS news (
							id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
							subject VARCHAR(50),
							news VARCHAR(500),
							deleted BOOLEAN,
							created DATETIME DEFAULT NULL,
							modified DATETIME DEFAULT NULL
      								)";
      			$cn->query($sql);

        /* allow add action so user can register */
        $this->Auth->allow('show');
    }
	
	public function show(){
		$this->Session->write('news', $this->News->find('all', array('conditions' => array('News.deleted =' => false))));
	}
	
	public function delete(){
		if ($this->request->is('get')) {
			$this->News->id = $this->request->params['pass']['0'];
			$this->News->saveField('deleted', true);
			
			$this->Session->setFlash(__('News wurde gelöscht!'));
			
			return $this->redirect(array('controller' => 'news','action' => 'show'));
		}
	}
	
	public function add(){
		if($this->Auth->user('rangID') == 1)
			return $this->redirect(array('controller' => 'news','action' => 'show'));
			
		if ($this->request->is('post')) {
			$this->News->create();

			if($this->News->save($this->request->data)){
				$this->News->saveField('deleted', false);
			
				$this->Session->setFlash(__('News angelegt!'));
				return $this->redirect(array('controller' => 'pages','action' => 'home'));
			}
		}
	}
}